package com.micai.springboot.web;

import com.micai.springboot.service.HelloWorldService;
import com.micai.springboot.task.RunnableTask1;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author 赵新国 on 2017/8/21.
 */
@RestController //提供实现了REST API，可以服务JSON,XML或者其他。这里是以String的形式渲染出结果。
@EnableAsync
public class HelloWorldController {

    private static final Logger LOGGER = LoggerFactory.getLogger(HelloWorldController.class);

    @Autowired
    private HelloWorldService helloWorldService;

    @RequestMapping("/") //提供路由信息，”/“路径的HTTP Request都会被映射到sayHello方法进行处理。
    public String sayHello(){
        return "Hello,World!";
    }

    /**
     * 同步处理
     * @return
     */
    @RequestMapping(value = "/test2",method = RequestMethod.GET)
    public String test2(){
        helloWorldService.getTest2();
        LOGGER.info(Thread.currentThread().getName()+"==========主线程名");
        return "同步,正在解析......";
    }

    /**
     * 异步处理1：线程池，创建新线程处理
     * @return
     */
    @RequestMapping(value = "/test3",method = RequestMethod.GET)
    public String test3(){
        ExecutorService service = Executors.newFixedThreadPool(5);
        RunnableTask1 task1 = new RunnableTask1();
        service.execute(task1);
        LOGGER.info("=========》当前线程名："+Thread.currentThread().getName());
        return "异步,正在解析......";
    }

    /**
     * 异步处理2：使用springBoot自带async注解
     */
    @RequestMapping(value = "/test4",method = RequestMethod.GET)
    public String test4(){
        helloWorldService.getTest4();
        return "异步,正在解析......";
    }

}
